package com.zyd.shiro.business.controller.pak;

import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Enter;
import com.zyd.shiro.business.entity.Order;
import com.zyd.shiro.business.service.*;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.persistence.beans.PakOrderLine;
import com.zyd.shiro.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import java.util.List;


@RestController
@RequestMapping("/enter")
public class PakEnterController {
    @Autowired
    private PakEnterService enterService;

    @Autowired
    private PakOrderService orderService;

    @Autowired
    private PakOrderLineService orderLineService;

    @Autowired
    private PakEnterLineService enterLineService;

    @Autowired
    private PakStockService stockService;


    @PostMapping
    @ApiOperation(value="添加", produces="application/json")
    public ResponseVO insert(Enter enter) {
        enterService.insert(enter);
        return ResultUtil.success("添加成功！",enter);
    }

    @PutMapping
    @ApiOperation(value="修改", produces="application/json")
    public ResponseVO update(Enter enter) {
        enterService.updateSelective(enter);
        return ResultUtil.success("修改成功！",enter);
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value="主键删除", produces="application/json")
    public ResponseVO delete(@PathVariable(name="id") Long id){
        enterService.removeByPrimaryKey(id);
        return ResultUtil.success("删除成功！",null);
    }

    @GetMapping
    @ApiOperation(value="分页查询", produces="application/json")
    public PageResult page(BaseConditionVO vo) {
        PageInfo<Enter> pageInfo = enterService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

    @GetMapping(value = "/all")
    @ApiOperation(value="查询所有", produces="application/json")
    public ResponseVO list() {
        List<Enter> enters = enterService.listAll();
        return ResultUtil.success("查询成功！",enters);
    }

  /*  @PostMapping(value = "/import")
    @ApiOperation(value="批量导入", produces="application/json")
    public ResponseVO folderImport(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.indexOf("."), fileName.length());
        if (suffix.equals(".xlsx") || suffix.equals(".xls")) {
            File newFile = new File(FileUtils.createFilePath(fileName));
            file.transferTo(newFile);
            ExcelUtil<PakEnter> excelUtil = new ExcelUtil<>();
            List<PakEnter> acfForms = excelUtil.importExcel("Sheet1", newFile, PakEnter.class);
            for (PakEnter a : acfForms) {
                a.setCreateTime(new Date());
                a.setUpdateTime(new Date());
                enterService.increase(a);
            }
            return ResultUtil.success("修改成功！");
        }
        return ResultUtil.error("请输入excel文件!");
    }*/


    @GetMapping(value = "/order/{id}")
    @ApiOperation(value="入库", produces="application/json")
    public ResponseVO warehousing(@PathVariable(name="id") Long id,Enter enter) {
        Order order = orderService.getByPrimaryKey(id);
        if(order.getPakOrder().getAppstatus()!=1){
            return ResultUtil.error("未通过审批！");
        }

        List<PakOrderLine> orderLines = orderLineService.findByOrder(id);
        for(PakOrderLine orderLine:orderLines){
            enterLineService.increase(orderLine,enter.getId());
            stockService.enterWare(orderLine,enter.getId());
        }
        order.getPakOrder().setWarestatus(1);
        orderService.update(order);
        return ResultUtil.success("查询成功！",order);
    }
}